<?php
Globals::requireController('Admin');
Globals::requireTable('Product');
Globals::requireTable('Days');
Globals::requireTable('Account');
Globals::requireTable('Order');
Globals::requireTable('Withdrawals');
Globals::requireTable('Msg');

class OrderController extends AdminController
{
    protected $productTable;
    protected $daysTable;
    protected $accountTable;
    protected $orderTable;
    protected $withdrawalsTable;
    protected $MsgTable;
    
	public function __construct($config = null)
	{
		parent::__construct($config, true);
		$this->productTable = new ProductTable($config);
		$this->daysTable = new DaysTable($config);
		$this->accountTable = new AccountTable($config);
		$this->orderTable = new OrderTable($config);
		$this->withdrawalsTable = new WithdrawalsTable($config);
		$this->MsgTable = new MsgTable($config);
	}
	
	public function indexAction()
	{
	    $where = array();
	    
	    $uid = $this->getIntParam('uid');
	    if($uid > 0){
	        $where['uid'] = $uid;
	        $this->view->uid = $uid;
	    }
	    
	    $aid = $this->getIntParam('aid');
	    if($aid > 0){
	        $where['aid'] = $aid;
	        $this->view->aid = $aid;
	    }
	    
	    $pid = $this->getIntParam('pid');
	    if($pid > 0){
	        $where['pid'] = $pid;
	        $this->view->pid = $pid;
	    }
	    
	    $count = $this->orderTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $data = $this->orderTable->listPage($where, 'id DESC', $pageSize, $pageId);
	    foreach ($data as $k=>$v){
	        $tmp_product = $this->productTable->getRow($v['pid']);
	        $data[$k]['product'] = $tmp_product;
	        
	        $tmp_account = $this->accountTable->getRow($v['aid']);
	        $data[$k]['account'] = $tmp_account;
	    }
	    $this->view->data = $data;
	    
	    global $order_status;
	    $this->view->order_status = $order_status;
	    
	    $product_info = $this->productTable->listAll(array('status'=>1));
	    $this->view->product_info = $product_info;
	}
	
	public function detailAction()
	{
	    $id = $this->getIntParam('id');
	    $order = $this->orderTable->getRow($id);
	    if($order['id'] > 0){
	        $order['user'] = $this->userTable->getRow($order['uid']);
	        $order['product'] = $this->productTable->getRow($order['pid']);
	        $order['account'] = $this->accountTable->getRow($order['aid']);
	        $this->view->order = $order;
	        
	        global $order_status;
	        $this->view->order_status = $order_status;
	    }else{
	        $this->redirectToAction();
	        exit();
	    }
	}
	
	public function withdrawalslistAction(){
	    $where = array();
	    $count = $this->withdrawalsTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $data = $this->withdrawalsTable->listPage($where, 'id DESC', $pageSize, $pageId);
	    $this->view->data = $data;
	     
	    global $withdrawals_status;
	    $this->view->withdrawals_status = $withdrawals_status;
	}
	
	public function refusewithdrawalsAction(){
	    $id = $this->getIntParam('id');
	    if($id > 0){
    	    $this->withdrawalsTable->update(array('status'=>2),$id);
    	    $this->redirectToAction('withdrawalslist');
	    }
	    exit();
	}
	
	public function agreewithdrawalsAction(){
	    $id = $this->getIntParam('id');
	    if($id > 0){
    	    $withdrawals = $this->withdrawalsTable->getRow($id);
    	    $id_arr = explode(',', $withdrawals['orderids']);
    	    foreach ($id_arr as $v){
    	        $this->orderTable->update(array('status'=>4),array('orderid'=>$v));
    	    }
    	    
    	    $this->withdrawalsTable->update(array('status'=>1),$id);
    	    
    	    $tmp_data['uid'] = $withdrawals['uid'];
    	    $tmp_data['createdate'] = date('Y-m-d H:i:s');
    	    $tmp_data['msg'] = '您的提现已成功！请打开支付宝，进入用户中心点击账单就可以看到了。';
    	    $tmp_data['status'] = 0;
    	    $this->MsgTable->add($tmp_data);
    	    
    	    $exreas = array(
    	        'content'=>'点击去看看打开支付宝账单就可以看到了',
    	        'pushtype'=>24
    	    );
    	    do_jpush($withdrawals['uid'],'小主您的提现金额已到账',$exreas);
    	    
	    }
	    $this->redirectToAction('withdrawalslist');
	    exit();
	}
	
	public function setunusualAction(){
	    $id = $this->getIntParam('id');
	    if($id > 0){
	        $this->orderTable->update(array('status'=>5),$id);
	    }
	    $this->redirectToAction('index');
	    exit();
	}
}
